package me.zhengjie.modules.web.service.pay.order;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.CourseClassStudent;
import me.zhengjie.modules.system.domain.CourseMealsOrder;
import me.zhengjie.modules.system.service.CourseClassService;
import me.zhengjie.modules.system.service.CourseMealsOrderService;
import me.zhengjie.modules.system.service.CourseOrderService;
import me.zhengjie.modules.system.service.dto.*;
import me.zhengjie.modules.system.service.mapstruct.CourseClassStudentExpMapper;
import me.zhengjie.modules.web.domain.pay.OrderSubmitRequest;
import me.zhengjie.modules.web.domain.pay.OrderSubmitResponse;
import me.zhengjie.modules.web.enums.OrderStatusEnum;
import me.zhengjie.modules.web.service.order.IOrderService;
import me.zhengjie.modules.web.service.pay.IOrderSubmitService;
import me.zhengjie.modules.weixin.utils.OrderUtil;
import me.zhengjie.request.ATMRContext;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.enums.OrderPrefixEnum;
import me.zhengjie.utils.enums.OrderTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MealsOrderSubmitServiceImpl implements IOrderSubmitService {
    @Autowired
    private CourseMealsOrderService courseMealsOrderService;

    @Autowired
    private CourseClassService courseClassService;

    @Autowired
    private CourseClassStudentExpMapper courseClassStudentExpMapper;

    @Autowired
    private CourseOrderService courseOrderService;

    @Autowired
    private IOrderService orderService;

    @Override
    public int orderType() {
        return OrderTypeEnum.Meals.getOrderType();
    }

    @Override
    public OrderSubmitResponse execute(OrderSubmitRequest request) {
        if (StringUtils.isBlank(request.getClassNo())){
            throw new WeChatException("缺少参数classNo");
        }
        CourseClassQueryCriteria criteria = new CourseClassQueryCriteria();
        criteria.setClassNo(request.getClassNo());
        criteria.setStatus(ConstantUtils.Valid);
        List<CourseClassDto> courseClassDtos = courseClassService.queryAll(criteria);
        if (CollectionUtil.isEmpty(courseClassDtos)){
            throw new WeChatException("参数传递有误");
        }
        /*CourseOrderQueryCriteria orderQueryCriteria = new CourseOrderQueryCriteria();
        orderQueryCriteria.setCourseNo(courseClassDtos.get(0).getCourseNo());
        orderQueryCriteria.setPayStatus(ConstantUtils.Order_Payed);
        List<CourseOrderDto> courseOrderDtos = courseOrderService.queryAll(orderQueryCriteria);
        if (CollectionUtils.isEmpty(courseOrderDtos)){
            throw new WeChatException("未购买课程不能签到哦");
        }*/
        String openId = ATMRContext.getContext().getOpenId();
        CourseClassDto courseClassDto = courseClassDtos.get(0);
        BigDecimal meals = courseClassDto.getMeals();
        String orderSn = OrderUtil.GetOrderNumber(OrderPrefixEnum.Meals.getPrefix());
        CourseMealsOrder mealsOrder = new CourseMealsOrder();
        mealsOrder.setMeals(meals);
        mealsOrder.setOpenId(openId);
        mealsOrder.setOrderNo(orderSn);
        mealsOrder.setCreateTime(DateUtil.TimestampNow());
        mealsOrder.setPayAmount(BigDecimal.ZERO);
        mealsOrder.setPaySerialid("");
        mealsOrder.setPrePayid("");
        mealsOrder.setClassNo(request.getClassNo());
        mealsOrder.setPayStatus(OrderStatusEnum.WaitingPay.getOrderStatus());
        mealsOrder.setOrderStatus(OrderStatusEnum.WaitingPay.getOrderStatus());
        Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now().plusMinutes(ConstantUtils.DefaultExpireTime));
        mealsOrder.setExpireTime(timestamp);
        if (meals.compareTo(BigDecimal.ZERO) == 0){
            mealsOrder.setPayStatus(OrderStatusEnum.Payed.getOrderStatus());
            mealsOrder.setOrderStatus(OrderStatusEnum.Payed.getOrderStatus());

            CourseClassStudent student = new CourseClassStudent();
            student.setMealsStatus(ConstantUtils.Meals_Payed);
            student.setUpdateTime(DateUtil.TimestampNow());
            LambdaQueryWrapper<CourseClassStudent> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CourseClassStudent::getClassNo,request.getClassNo()).eq(CourseClassStudent::getOpenId, openId);
            courseClassStudentExpMapper.update(student,wrapper);
        }
        CourseMealsOrderDto examOrderDto = courseMealsOrderService.create(mealsOrder);
        OrderSubmitResponse response = new OrderSubmitResponse();
        response.setOrderId(examOrderDto.getOrderId());
        response.setOrderSn(orderSn);
        response.setOrderStatus(mealsOrder.getOrderStatus());

        orderService.orderExpire2Redis(orderType(),orderSn,timestamp);
        return response;
    }
}
